package com.yenroc.ho.service;


import com.yenroc.ho.annotation.GlobalTransaction;
import com.yenroc.ho.feign.GoodsFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class OrderService {

    private Logger log = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private GoodsFeign goodsFeign;

    @GlobalTransaction
    public Map<String, Object> addOrder(Map<String, Object> params) throws Exception {
        log.info("进行下单操作..."+params);

        // 进行减库存调用
        log.info("进行减库存调用");
        Map<String, Object> stringObjectMap = goodsFeign.goodsReduce(params);
        log.info("减库存成功={},开始创建订单..", stringObjectMap);

        Integer goodId = (Integer)params.get("goodId");
        Integer payNum = (Integer)params.get("payNum");
        jdbcTemplate.update("insert into tk_order(good_id,pay_num) values(?,?)",goodId, payNum);
        log.info("下单成功..");

        System.out.println("1/0="+ 1/0);
        Map<String, Object> result = new HashMap<>();
        result.put("result","下单成功");
        return result;
    }

}
